<?php

namespace app\api\controller\app;

use addons\epay\library\Service;
use app\admin\model\Notice;
use app\admin\model\Org;
use app\admin\model\Roomclean;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopcoupon;
use app\admin\model\Shopfav;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shoproomset;
use app\admin\model\Shopwifi;
use app\admin\model\Userbalance;
use app\admin\model\Usercoupon;
use app\admin\model\Userorderprofit;
use app\admin\model\Userweixin;
use app\api\service\VbillService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use EasyWeChat\Factory;
use fast\Random;
use think\Config;
use think\Validate;
use Yansongda\Pay\Pay;
use function EasyWeChat\Kernel\Support\get_client_ip;

/**
 */
class Coupon extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $app = null;

    public function _initialize()
    {
        parent::_initialize();

    }

    public function userCouponList(){

        $user = $this->auth->getUser();
        $userCouponModel = new Usercoupon();
        //error_log("====userCouponList==".$user['id']);
        $shopRoomModel= new Shoproom();
        $shopModel = new \app\admin\model\Shop();
        $list = $userCouponModel->with(['coupon'])->where('Usercoupon.status','unuse')->where('user_id',$user['id'])->select();
        foreach ($list as $key=>$val){
            $list[$key]['create_time_txt'] = date("Y-m-d",$val['create_time']);
            $list[$key]['valid_time_txt'] = date("Y-m-d",$val['valid_time']);



            $room = json_decode($val['coupon']['rule'],true)['room'];

            $rooms = array();
            foreach ($room as $k=>$v){
                $obj = array();
                $r =  $shopRoomModel->where('id',$v)->find();
                $s = $shopModel->where('id',$r['shop_id'])->find();
                $obj['shop_name'] = $s['name'];
                $obj['room_name'] = $r['name'];
                $rooms[] = $obj;
            }
            $list[$key]['rooms'] = $rooms;


            $list[$key]['check'] = false;
            $tag= json_decode($val['coupon']['rule'],true)['notice'];
            $tags = array();
            $o = array();
            $o['name'] = "使用时间：".json_decode($val['coupon']['rule'],true)['time'];
            $tag[] = $o;

            $week = json_decode($val['coupon']['rule'],true)['week'];
            $w = "";
            foreach ($week as $k=>$v){
                $w .= "周".$v;
                if ($k != count($week) - 1) {
                    $w .= ",";
                }
            }
            $o = array();
            $o['name'] = "使用日期:".$w;
            $tag[] = $o;

            foreach ($tag as $k => $v) {
                $obj = array();
                $obj['id'] = ($k + 1);
                $obj['value'] = $v['name'];
                $tags[] = $obj;
            }
            $list[$key]['rule_txt'] = $tags;

        }
        $this->success('',$list);
    }


    public function getShopCouponList(){

        $shop_id = $this->request->param('shop_id');
        $type = $this->request->param('type');
        $where = array();
        if($type != ""){
            $where['type'] = 'normal';
        }
        $shopCouponModel = new Shopcoupon();
        $list = $shopCouponModel->where('shop_id',$shop_id)->where($where)->where('status','doing')->select();
        $shopRoomModel = new Shoproom();
        $shopModel =new \app\admin\model\Shop();
        $userCouponModel = new Usercoupon();
        foreach ($list as $key=>$val){
            $userCopon = $userCouponModel->where('coupon_id',$val['id'])->find();
            $list[$key]['is_take'] = -1;
            if($userCopon){
                if($userCopon['status'] == 'unuse'){

                    $list[$key]['is_take'] = 1;
                }
            }
            $list[$key]['create_time_txt'] = date("Y-m-d",$val['create_time']);
            $list[$key]['valid_time_txt'] = date("Y-m-d",strtotime("+".$val['valid_day']." day",$val['create_time']));

            $list[$key]['check'] = false;
            $tag= json_decode($val['rule'],true)['notice'];
            $tags = array();
            $o = array();
            $o['name'] = "使用时间：".json_decode($val['rule'],true)['time'];
            $tag[] = $o;

            $week = json_decode($val['rule'],true)['week'];
            $w = "";
            foreach ($week as $k=>$v){
                $w .= "周".$v;
                if ($k != count($week) - 1) {
                    $w .= ",";
                }
            }
            $o = array();
            $o['name'] = "使用日期:".$w;
            $tag[] = $o;

            foreach ($tag as $k => $v) {
                $obj = array();
                $obj['id'] = ($k + 1);
                $obj['value'] = $v['name'];
                $tags[] = $obj;
            }
            $list[$key]['rule_txt'] = $tags;

            $room = json_decode($val['rule'],true)['room'];

            $rooms = array();
            foreach ($room as $k=>$v){
                $obj = array();
                $r =  $shopRoomModel->where('id',$v)->find();
                $s = $shopModel->where('id',$r['shop_id'])->find();
                $obj['shop_name'] = $s['name'];
                $obj['room_name'] = $r['name'];
                $rooms[] = $obj;
            }

            $list[$key]['rooms'] = $rooms;
        }

        $this->success('',$list);

    }

    public function takeCoupon(){

        $id = $this->request->param('id');
        $user = $this->auth->getUser();

        $shopCouponModel = new Shopcoupon();
        $coupon = $shopCouponModel->where('id',$id)->find();
        $userCouponModel = new Usercoupon();
        $endTime = strtotime("+".$coupon['valid_day']." day",time());
        $userCouponModel->insert(['user_id'=>$user['id'],'coupon_id'=>$coupon['id'],'create_time'=>time(),'status'=>'unuse','valid_time'=>$endTime]);


        $this->success('领取成功');
    }


}
